

///////////////////////////////////////////////////////
/*
  Project:	Charge10
  Platform: HC32F4A0
  Author:   Donnie Liang
  Data:     2023.03.15
  Version:  V2.0
  FileName: CCT_Logic.c
	
//Concentor---CCT------	
	

*/
///////////////////////////////////////////////////////

#include  <stdio.h>
#include  "string.h"


#include "f133_drv_config.h"
#include "config.h"


#include  "../user/global.h"
#include  "../user/TimeBase.h" 
#include "timer_app.h"


#include  "../device/GPRS.h"

#include  "tcp_client_app.h" 


#include "Param.h"


#include  "CCT_TopProtocol.h"

#include  "CCT_Logic.h"


 CCT_Obj_st        CCT_Obj ;
 Server_RxData_st  Server_RxData;


volatile uint8_t       g_ucReciveServerFlag;     /* 接收到服务器有效命令 */       
volatile uint16_t      g_ucReciveServerLen;     


 //通信接口通道编号,1=GPRS,2以太网，3---PC串口

Server_Chn_e   gucServer_Tx_Chn  = SERVER_CH_NULL ;   //channel   上行通信接口通道编号

uint8_t g_ucConcentratorState[17]={0x00,0x01,0x02,0x03,0x04};         // [0] 表示报警个数 ---8(报警信息)	



void CCT_Logic_Init(void)
{
	memset(&CCT_Obj,0,sizeof(CCT_Obj_st) );
	
    CCT_Obj.Status = INITIAL;
	
	CCT_Obj.u32RunTime_Sec = 0;
	CCT_Obj.ReLogon        = 0;
	CCT_Obj.ucLoginCnt     = 0;	
	CCT_Obj.ucSystemRest   = 0;	

  // g_uiAlarm_Sec = 0;	
	
	memset(&Server_RxData,0,sizeof(Server_RxData_st) );
	
	 Server_RxData.Buff = Tcp_RxData.Buff;
	
	 gucServer_Tx_Chn = SERVER_CH_NULL ; 	
	
}



//---查询集中器是否在线------
//返回0---集中器不在线，1--集中器在线
uint8_t  IsCCT_Online(void)
{
	
	  if(NORMAL_WORK == CCT_Obj.Status)
		{
		   return 1 ;
		}else{
			
		   return 0 ;
		}
		
}




uint8_t  GetCCT_State(void)
{
	return CCT_Obj.Status ;
	
}


void  ResetCCT_State( void )////debug 0x20,0x11,0x03,0x38 
{
	  CCT_Obj.Status  = INITIAL ;
	//  g_ucCCT_Status = LOGINING ;////debug 0x20,0x11,0x03,0x38   
	
}
           

void  SetCCT_State( CCT_Status_e   Status	)
{
	  CCT_Obj.Status  = Status ;
}


void  ResetCCT_Heart( void )
{
	 CCT_Obj.ucHeartBeatTry  = 0;   
}

void  ClearCCT_Heart( void )
{
	 CCT_Obj.ucHeartBeatSec  = 0;   
}

void  CCT_ReLogon( void )
{
	  CCT_Obj.ReLogon  = 1 ;
}



void  CCT_SystemRestCmd( void )
{
	
	  SetTimer( 1, CCT_STATUS_TIMER, 20);
	
	  CCT_Obj.ucSystemRest  = 1 ;

}




///////////---------6.4.7  B :集中器主动上报掉电事件 EEH---------------------------------------------------------------------

					


//------周期 1sec---------
void CCT_Logist_Tick(void)
{		
	CCT_Obj.ucLoginSec++;									
	CCT_Obj.ucHeartBeatSec++;													
    CCT_Obj.u32RunTime_Sec++; 	
		
}
				 
							


void CCT_Logic_RelogChecking(void)
{
 
	if( !CCT_Obj.ReLogon )
	{
       return ;
	}


	Param_Init( );

	ResetCCT_State( );	

	GPRS_Init();		
	// GPRS_Reset( );  

	//xxx_Config()方法要在所有的xxx_Init()方法之后才能运行  
	GPRS_Config( );

	Tcp_Client_App_Init( );
	

	//........ver4.30..................
		GPRS_CoolReset( );	
	// GPRS_HotReset( );	
			
		TCP_Reset( );
	//........ver4.30..................


	CCT_Obj.ReLogon    = 0;
	CCT_Obj.ucLoginCnt = 0;  


}






void  CCT_SystemResetChecking(void)
{
	
		if( 0==CCT_Obj.ucSystemRest )
			 return;
		
		if( 0 != GetTimer( CCT_STATUS_TIMER) )
		{
				return;	
		}

		
// __disable_irq();	//ver.62 		
		
		//可以增加以下复位的逻辑
	//	 ChargLogic_PowerDown_ISR( );
		
	//	 Delay_ms(5000);//ver.62-
		
	//	 NVIC_SystemReset();	


}







 //------16路充电平台-------
	

void CCT_Logic_Process(void)
{
	 CCT_SystemResetChecking( );
	 CCT_Logic_RelogChecking( );
	
    switch( CCT_Obj.Status )
    {
        case INITIAL: 
				{				
								 
           // LED_LinkOff( );

					  if( ( 0 != gucGPRS_ConfigDone) ||( 0 != IsEthernetLinkup() ) )
						{
							// SetTimer( 1, CCT_STATUS_TIMER, 60000);
                             CCT_Obj.Status   = WAITTING; 
							 //SetCCT_State( WAITTING );
						}
						
				}  break;
        
				
	      case WAITTING:			
				{								   
                          //  LED_LinkOff( );

							 CCT_Obj.ucLoginSec     = LOGIN_SEC; 
                             CCT_Obj.ucLoginCnt     = 0;
							 CCT_Obj.ucLoginAckFlag = 0;
							
							 CCT_Obj.Status   = LOGINING; 
						
				}  break;
				
				
        case LOGINING:
				{
		            if ( 0 != CCT_Obj.ucLoginAckFlag )  
					  {    					
                                ChargeUpdateInit(  );

								CCT_Obj.ucLoginAckFlag  = 0;
							    CCT_Obj.ucLoginSec  = LOGIN_SEC;  
								CCT_Obj.ucLoginCnt  = 0;							  	
							    CCT_Obj.ucGetTimeAckFlag  = 0;
															
							    CCT_Obj.ucHeartBeatSec = 0; 


 
#if   STREET_LAMP_SERVER_ENABLE              
      ///集中器接路灯服务器

								CCT_Obj.Status = NORMAL_WORK;
#elif CHARGE16_SERVER_ENABLE            
      //集中器接16路充电服务器
	                             CCT_Obj.Status = GET_TIME;

#else
	                             CCT_Obj.Status = NORMAL_WORK;
#endif
					
								//  LED_LinkOn();			
							
                    } else{ 
																
								if ( CCT_Obj.ucLoginSec >= LOGIN_SEC )
								{ //登录时间到									
										CCT_Obj.ucLoginSec = 0;    
			
										CCT_Obj.ucLoginCnt++;   
									
										SendLogin( gucCCT_ID );  //发登录包---  Box86List[0].ID  gucCCT_ID
									 								 
										if ( CCT_Obj.ucLoginCnt > LOGIN_CNT_MAX )  
										{//登录计数超出
											
											CCT_ReLogon( );													
										}									 
								}
	
						}					
						
					} break;

					




#if CHARGE16_SERVER_ENABLE       //集中器接16路充电服务器

				case GET_TIME:
				{		
					
					// req:68 00 10 00 50 03 80 68 00 01 8E 42 16
					// ack:68 00 10 00 50 03 80 68 80 09 8E 07 E2 0C 0C 03 0C 0C 0C F2 16
					//时间： 2018-12-12-03-12-12-12
					
		        if ( 0 != CCT_Obj.ucGetTimeAckFlag )  
					  {    
								CCT_Obj.ucGetTimeAckFlag  = 0;
							
							  CCT_Obj.ucHeartBeat     = 0;
                               CCT_Obj.ucHeartBeatSec  = 0;
							  CCT_Obj.ucHeartBeatTry  = 0;
						  	  CCT_Obj.ucLoginSec      = 0;  						
							
							    printf("RTC_Now:");
								RTC_Prinft( &RTC_Now );
							
							  uc_CCT_auto_send_step = 0 ;
							
								CCT_Obj.Status = AUTO_SEND; 
													
            } else{ 
												
							
								if ( CCT_Obj.ucLoginSec >= LOGIN_SEC )
								{ //								
										CCT_Obj.ucLoginSec = 0;    
			
										CCT_Obj.ucLoginCnt++;   
									
										GetServerTime( gucCCT_ID );  //请求服务器时间
									
										if ( CCT_Obj.ucLoginCnt > 3 )  
										{//请求计数超出
											
												CCT_Obj.ucLoginCnt = 0;    
	                     										
												CCT_Obj.Status = AUTO_SEND;			 //NORMAL_WORK			
										}											
																	
								}
	
						}					
						
					} break;
		
					
					
		//--------主动上报信息-----------------------------------				
        case AUTO_SEND:
				{					
									
					if ( CCT_Obj.ucLoginSec < 2 )
					{ //													
							break;
					}
					
						
					CCT_Obj.ucLoginSec = 0;  
						
					 if ( 0 != CCT_AutoSendProcess( ) )
					 { 				 
							CCT_Obj.Status = NORMAL_WORK; 
					 }
											


				} break;	
					
				
#endif					



 //---------------上报命令设计有优先级---避免数据粘包------
						
        case NORMAL_WORK:
		{																														
			//-------send to server-------------------											
	
#if SMART_ENABLE		
					
				if ( 0!= IsCCT_Upgrading( ) )
				{//如果集中器正在升级---则不主动上传数据包
					break;
				}
#endif
							 
							 
				////-----0x22,0x02,0x23,0x66--的逻辑----
				if ( CCT_Obj.ucHeartBeatSec >= HEART_BEAT_SEC )
				{ //								
						CCT_Obj.ucHeartBeatSec = 0;    		
							
						CCT_Obj.ucHeartBeat++;  
					
						SendHearBeat( gucCCT_ID, CCT_Obj.ucHeartBeat);  //发心跳包---  Box86List[0].ID  gucCCT_ID
					
						CCT_Obj.ucHeartBeatTry++;  
						if ( CCT_Obj.ucHeartBeatTry > HEART_BEAT_CNT_MAX )  
						{//请求计数超出
							
							CCT_Obj.ucHeartBeatTry = 0;    
							//CCT_ReLogon( );											
							ResetCCT_State( );		//重新登录				
						}											

				}else{  //---空闲发送心跳包------

#if STREET_LAMP_SERVER_ENABLE  //---------集中器接路灯服务器------------------------------------------------------

						CCT_Logic_Lamp_NormalWork( );

#endif  //---------------------------------------------------------------							


#if CHARGE16_SERVER_ENABLE  //---------集中器接16路充电服务器------------------------------------------------------

						CCT_Logic_Charge_NormalWork( );

#endif  //---------------------------------------------------------------	


							//........				
					}							



			} break;  // case NORMAL_WORK:
				
														
					
	}  //switch
								




	if(  NORMAL_WORK != GetCCT_State( ) )
	{		
		  CCT_OfflineProcess( );				
	}
			


}








